#!/usr/bin/env python
#-*- coding:utf-8 -*-
from django import forms
from crm import models
from django.core.exceptions import ValidationError
import hashlib

class RegForm(forms.ModelForm):
    password=forms.CharField(min_length=6,widget=forms.PasswordInput(
        attrs={'placeholder': '输入密码', 'oncontextmenu': 'return false', 'onpaste': 'return false'}))
    re_password = forms.CharField(min_length=6, widget=forms.PasswordInput(
        attrs={'placeholder': '输入确认密码', 'oncontextmenu': 'return false', 'onpaste': 'return false'}))
    class Meta:
        model = models.UserProfile
        fields='__all__'
        widgets={
            'username':forms.EmailInput(attrs={'placeholder':'您的用户名','autocomplete':'off'}),
            # 'password':forms.PasswordInput(attrs={'placeholder':'输入你的密码','autocomplete':'off'}),
            'mobile':forms.TextInput(attrs={'placeholder':'输入手机号码','autocomplete':'off'}),
            'name': forms.TextInput(attrs={'placeholder': '您的真实姓名', 'autocomplete': 'off'})
        }
        error_messages={
            'username': {
                'required': '必填',
                'invalid': '邮箱格式不正确',
        }
        }


        #定义全局钩子
    def clean(self):
        password=self.cleaned_data.get('password','')  #self.cleaned_data.get存放正确的地址
        re_password=self.cleaned_data.get('re_password','')
        if password == re_password:
            md5=hashlib.md5()
            md5.update(password.encode('utf-8'))
            self.cleaned_data['password']=md5.hexdigest()
            return self.cleaned_data
        else:
            self.add_error('re_password','两次密码不一致')
            raise ValidationError('两次密码不一致')

class CustomerForm(forms.ModelForm):
    class Meta:
        model=models.Customer
        fields='__all__'

